package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController("adminDishController")
@Api(tags = "菜品控制类")
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品和菜品口味")
    public Result<String> insert( @RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishService.insertWidthFlavor(dishDTO);
        String key="dish_"+dishDTO.getCategoryId();
        redisTemplate.delete(key);
        return  Result.success();
    }

    /**
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> pageQuery( DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询：{}",dishPageQueryDTO);
        PageResult pageResult=dishService.pageQueryWidthCategory(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids){
        log.info("批量删除菜品：{}",ids);
        dishService.deleteByList(ids);
        clearRedis("dish_*");
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> findDish(@PathVariable Long id){
        log.info("根据id查询菜品:{}",id);
        DishVO dishVO= dishService.findById(id);
        return Result.success(dishVO);
    }
    @PutMapping
    @ApiOperation("根据id修改菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("根据id修改菜品",dishDTO);
        dishService.update(dishDTO);
        clearRedis("dish_*");
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List> findByCateGoryId(Long categoryId){
        log.info("根据分类id查询菜品");
        List<Dish> dishes = dishService.findByCateGoryId(categoryId);
        return Result.success(dishes);
    }

    /**
     * 根据id修改菜品状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品起售状态")
    public Result changeStatusById(@PathVariable Integer status,Long id){
        dishService.changeStatus(status,id);
        clearRedis("dish_*");
        return Result.success();
    }
    private void clearRedis(String patten){
        Set keys = redisTemplate.keys(patten);
        redisTemplate.delete(keys);

    }
}
